\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{textcomp}

%%  Latex generated from POD in document /usr/local/share/perl/5.8.8/Inline/Struct.pod
%%  Using the perl module Pod::LaTeX
%%  Converted on Sun Feb 18 12:48:21 2007


\usepackage{makeidx}
\makeindex


\begin{document}

\tableofcontents

\section{NAME\label{NAME}\index{NAME}}


Inline::Struct -- Manipulate C structures directly from Perl.

\section{SYNOPSIS\label{SYNOPSIS}\index{SYNOPSIS}}
\begin{verbatim}
  use Inline C => Config => Structs => ['Foo'];
\end{verbatim}
\begin{verbatim}
  my $obj = Inline::Struct::Foo->new;
  $obj->num(10);
  $obj->str("Hello");
\end{verbatim}
\begin{verbatim}
  myfunc($obj);
\end{verbatim}
\begin{verbatim}
  __END__
  __C__
\end{verbatim}
\begin{verbatim}
  struct Foo {
    int num;
    char *str;
  };
\end{verbatim}
\begin{verbatim}
  void myfunc(Foo *f) {
    printf("myfunc: num=%i, str='%s'\n", f->num, f->str);
  }
\end{verbatim}


This complete program prints:

\begin{verbatim}
  myfunc: num=10, str='Hello'
\end{verbatim}
\section{DESCRIPTION\label{DESCRIPTION}\index{DESCRIPTION}}


Inline::Struct is not a new language. It's a language extension designed to 
be used by Inline::C. It parses struct definitions and creates
typemaps and XS code which bind each struct into a Perl class. This code is
passed to Inline::C, which compiles it in the normal way.



NOTE: Inline::Struct parses only C-style structs. It doesn't know about any
C++ extensions to structs like scopes, constructors or methods. If you want
such functionality you should use Inline::CPP to parse your structs.

\section{Using Inline::Struct\label{Using_Inline::Struct}\index{Using Inline::Struct}}


Inline::Struct has a Parse::RecDescent grammar to parse C structs. If a struct
is recognized, it can be bound to Perl. If the struct's definition is not
recognized (usually because it has a member with no typemap), it will not be
bound to Perl, but will be available from other functions in C or C++.



The following example shows how a simple struct might look to a Perl
programmer.



Example 1:

\begin{verbatim}
  use Inline C => <<'END', ENABLE => 'STRUCTS';
  struct Fraction {
    long numer;
    long denom; 
  };
  END
\end{verbatim}
\begin{verbatim}
  my $o = Inline::Struct::Fraction->new(4, 3);
  print $o->numer, $o->denom, "\n";
  $o->numer(4)->denom(7);
\end{verbatim}


After the code above has been compiled, Perl's namespace looks a lot like
the following:

\begin{verbatim}
  package Inline::Struct::Fraction;
  sub new { }
  sub DESTROY { }
  sub _KEYS { }
  sub _VALUES { }
  sub _HASH { }
  sub numer { }
  sub denom { }
\end{verbatim}


Note that these are actually XS subs written in C, not Perl subs. But that's 
what it looks like.

\section{The Struct Interface\label{The_Struct_Interface}\index{The Struct Interface}}


The following sections define the interface of each subroutine. \textbf{Note: this 
interface is likely to change in future versions of Inline::Struct}. Please
don't rely on Inline::Struct in production code quite yet.



When a struct is bound by Inline::Struct, a new namespace is created underneath
Inline::Struct. So if you have a struct named 'Foo', the package of the Perl
class will be 'Inline::Struct::Foo'.

\subsection{new\label{new}\index{new}}


If no arguments are provided, all fields are zeroed out. If you provide values,
they should be appropriate for the field type, and in the same order as they
are defined in the struct.

\subsection{DESTROY\label{DESTROY}\index{DESTROY}}


The destructor. Should never be called by the programmer -- this is called 
automatically when the Perl variable holding the struct is destroyed. Frees
the memory associated with the struct. If the struct holds pointers to malloc'd
memory, they will not be freed. If you run into such a situation, consider 
using C++ and Inline::CPP instead.

\subsection{\_KEYS\label{_KEYS}\index{\ KEYS}}


A read-only method, this returns a reference to an array containing the names
of the fields in the struct. The fields are in the order they appear in the 
C source code.

\subsection{\_VALUES\label{_VALUES}\index{\ VALUES}}


A read-only method, this returns a reference to an array containing the values
of the fields in the struct. The values are returned in the same order as the
fields.

\subsection{\_HASH\label{_HASH}\index{\ HASH}}


A read-only method, this returns a reference to a hash, mapping field names
to field values.

\subsection{Accessors\label{Accessors}\index{Accessors}}


For each field in the struct, an accessor sub will be created which lets you 
get or set the value in the struct. If no arguments are provided, the sub
returns the value of that field. If any arguments are provided, the field is
set to the first argument, and the modified structure is returned. This makes
setting multiple fields easy:

\begin{verbatim}
   $o->field1(something)->field2(somethingelse);
\end{verbatim}
\section{C and C++ Configuration Options\label{C_and_C_Configuration_Options}\index{C and C++ Configuration Options}}


Inline::Struct has no configuration options of its own, but it does provide
a new configuration option for C or C++.

\subsection{STRUCTS\label{STRUCTS}\index{STRUCTS}}


Specifies that structs are to be bound to Perl. There are several meanings to
this option, so I'll explain with an example:

\begin{verbatim}
   use Inline C => Config => STRUCTS => 'Foo';
\end{verbatim}


Adds 'Foo' to the list of structs to bind to Perl.

\begin{verbatim}
   use Inline C => Config => STRUCTS => ['Foo', 'Bar'];
\end{verbatim}


Adds 'Foo' and 'Bar' to the list of structs to bind to Perl.

\begin{verbatim}
   use Inline C => Config => STRUCTS => undef;
\end{verbatim}


Clears the list of structs to bind to Perl.

\begin{verbatim}
   use Inline C => Config => ENABLE => 'STRUCTS';
or
   use Inline C => Config => STRUCTS => 1;
\end{verbatim}


Enable binding structs to Perl, without specifying any structs to search for. 
As shown, this would bind all structs to Perl.

\begin{verbatim}
   use Inline C => Config => DISABLE => 'STRUCTS';
or
   use Inline C => Config => STRUCTS => 0;
\end{verbatim}


Disable binding structs to Perl.

\section{SEE ALSO\label{SEE_ALSO}\index{SEE ALSO}}


For more information about using C from Perl, see \emph{Inline::C}. For more
information about using C++ from Perl, see \emph{Inline::CPP}.

\section{AUTHOR\label{AUTHOR}\index{AUTHOR}}


Neil Watkiss (NEILW@cpan.org)

\section{COPYRIGHT\label{COPYRIGHT}\index{COPYRIGHT}}


Copyright (C) 2001, Neil Watkiss.



This module is free software. It may be used, redistributed and/or modified
under the same terms as Perl itself.



(see http://www.perl.com/perl/misc/Artistic.html)

\printindex

\end{document}
